//user.js
let Models = require('../models')

module.exports = {
    login: function(request, reply) {

        console.log(request);

        var name = request.payload.name || "";
        var pwd = request.payload.pwd || "";

        if (name.length == 0 || name.pwd == 0) {
            return {
                code: 1,
                message: '参数缺失',
                data: null
            }
        }

        return Models.user_info.findAll({
            where: {
                user_name: name
            }
        }).then(function(result) {
            let reponseMess = {
                code: 0,
                message: '操作失败',
                data: result
            };

            if (result == null || result.length == 0) {
                return {
                    code: 1,
                    message: '用户不存在',
                    data: null,
                    pwd: pwd
                }
            }

            var mod = result[0];
            if (mod.pwd == pwd) {

                reponseMess = {
                    code: 1000,
                    message: '登陆成功',
                    data: mod,
                    token: 'ABC'
                };
            } else {
                reponseMess = {
                    code: 2,
                    message: '密码错误',
                    data: null
                };
            }

            return reponseMess;
        });
    },
    login_one: function(request, reply) {
        return Models.user.findAll({

        }).then(function(result) {
            return {
                code: 100,
                message: 'success',
                data: result,
                req_body: request.body,
                req_query: request.query
            };
        });


    },
    query: function(request, reply) {
        return Models.user.findAll({
            where: {
                name: request.query.name
            }
        }).then(function(result) {
            let reponseMess = {};
            if (result !== null) {
                reponseMess = {
                    code: 100,
                    message: 'success',
                    data: result
                }
            } else {
                reponseMess = {
                    code: -100,
                    message: 'fail',
                    data: ''
                }
            }
            return reponseMess;
        });
    }
};